const moment = require('moment');
const request = require('request');
const fs = require('fs');
const path = require('path');
const iconvLite = require('iconv-lite'); // 编码转换
const cheerio = require('cheerio'); // 仿jQuery操作Dom元素

const utilTool = require('../../util');
const $config = require('../../config');

function requestPromise(url) {
    return new Promise((resolve, reject) => {
        request(url, { encoding: null }, function(error, response, content) {
            if (response.statusCode === 200) {
                const html = iconvLite.decode(content, 'utf8').toString('utf8');
                resolve(html);
            } else {
                reject(error)
            }
        })
    })
}

module.exports = {
  reptile: async (ctx, next) => {
    $config.documents = 'site' // 切换文档集合
    const { website, webpath, webattr } = ctx.request.query;

    const data = await requestPromise(website);
    const $ = cheerio.load(data);
    const hrefList = [];
    // .hy-layout > .clearfix .col-md-7 .hy-video-list .item+.col-md-3 a
    $(webpath).each((i, item) => {
        hrefList.push({
            title: $(item).attr('title') || '无',
            original: website + $(item).attr('data-original'),
            href: website + $(item).attr(webattr),
        })
    })

    // 多数据累加触发避免并发
    // const arr = []
    // arr.reduce((res, url) => {
    //     return res.then(() => {
    //         return new Promise(async (resolve) => {
    //             await getFun();
    //             resolve();
    //         })
    //     })
    // }, Promise.resolve())


    fs.writeFile($p('public/reptile/index.json'), JSON.stringify(hrefList), function(err) {
        if(err) console.log(err);
        else console.log('写文件操作成功');
    })
    // ctx.body = {
    //     list: hrefList,
    // };
    await ctx.render('reptile', {
        title: '爬虫API',
        list: {
            title: ['电影名字', '图片','链接'],
            tbody: hrefList,
        },
    })
  }
};
